
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.mac.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.mac.entity.Mac;
import com.application.mac.struts.form.MacForm;
import com.application.mac.service.MacService;
import com.application.mac.commons.Constants;

/**
 * 
 * Description:MacAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class MacAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private MacService macService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Mac mac;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getMacPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchMac" + "开始");
		MacForm mac = (MacForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List macList = null;
		resultSize = macService.getAllCountMac(mac);
		pageBean = PageBean.getPageBean("macTab", resultSize, request);
		macList = macService.searchMac(mac,
				pageBean);
		request.setAttribute("macList", macList);

		log.debug("searchMac" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddMac(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddMacs");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addMac(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addMacs");
		Mac mac = this.getMac(form);
		mac.setId(RandomKey.generateID_Long_19().toString());
		mac.setCreateDate(new Date());

		this.macService.insertObject(mac);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteMac(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteMacs");
		MacForm macForm = (MacForm) form;
		this.macService.deleteMacs(macForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateMac(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateMacs");
		MacForm macForm = (MacForm) form;
		mac = (Mac) this.macService.searchMac(
				macForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, mac);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateMac(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateMacs");
		Mac mac = this.getMac(form);
		mac.setCreateDate(new Date());
		this.macService.updateObject(mac);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewMac(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewMac");
		MacForm macForm = (MacForm) form;
		mac = (Mac) this.macService.searchMac(
				macForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, mac);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the MacService
	 */
	public MacService getMacService() {
		return this.macService;
	}

	/**
	 * @param MacService
	 *            the MacService to set
	 */
	public void setMacService(MacService macService) {
		this.macService = macService;
	}

	/**
	 * 获取Mac实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Mac
	 */
	public Mac getMac(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		MacForm macForm = (MacForm) form;
		mac = new Mac();
		BeanUtils.copyProperties(mac, macForm);
		return mac;
	}

	public void setMac(Mac mac) {
		this.mac = mac;
	}

//	public ActionForward creatTreeSelect(ActionMapping mapping,
//			ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) throws IOException {
//		response.setContentType("text/text;charset=UTF-8");
//		// System.out.println(request.getParameter("pid"));
//		MacForm macForm = new MacForm();
//		macForm.setFid(request.getParameter("pid"));
//		String vid = request.getParameter("treeid");
//		int tjs = Integer.parseInt(request.getParameter("js"));
//		Long pid = Long.parseLong(request.getParameter("pid"));
//		List<Mac> accList = macService
//				.searchMac(macForm);
//		String html = creatTreeSelect(accList, vid, tjs);
//		java.io.PrintWriter out = response.getWriter();
//		out.print(html);
//		out.flush();
//		out.close();
//		return null;
//	}
//
//	public String creatTreeSelect(List<Mac> accList, String vid, int js) {
//		String html = "";
//		for (int i = 0; i < accList.size(); i++) {
//			Mac mac = (Mac) accList.get(i);
//			for (int j = 0; j < js; j++) {
//				html = html + "<img src='images/line.gif'>";
//			}
//			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
//					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + mac.getId() + "');\" >"
//					+ "<img src='images/close1.gif' id='img" + vid + js + i
//					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + mac.getId()
//					+ "');\" style=\"cursor:hand\">&nbsp;";
//
//			html = html + "<a onclick=\"selectTree('" + mac.getId()
//					+ "','" + mac.getName()
//					+ "')\" style=\"cursor:hand\">" + mac.getName()
//					+ "</a>";
//			html = html + "<div id='" + vid + js + i
//					+ "' ><span class='f1'>.</span></div>";
//		}
//		return html;
//	}

}
